package com.rstar.readerbook.gesture;

import com.rstar.readerbook.gesture.ScreenUtil.Screen;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class GestureSlideExt {
    /** 
     * 滑屏方向：向上 
     */ 
	public static final int GESTURE_UP 		= 1;
	
    /** 
     * 滑屏方向：向右
     */ 
	public static final int GESTURE_RIGHT	= 2;
	
	/** 
     * 滑屏方向：向下 
     */ 
	public static final int GESTURE_DOWN 	= 3;
	
    /** 
     * 滑屏方向：向左 
     */ 
	public static final int GESTURE_LEFT	= 4;
	
	private Context mContext;
	private Screen screen;
	private OnGestureResult onGestureResult;
	
	public GestureDetector Buile() {
        return new GestureDetector(mContext, onGestureListener);
    }
	
	public GestureSlideExt(Context context, OnGestureResult onGestureResult) {
        this.mContext = context;
        this.onGestureResult = onGestureResult;
        screen = ScreenUtil.getScreenPix(context);
    }
	
	public void doResult(int result) {
        if(onGestureResult != null) {
            onGestureResult.onGestureResult(result);
        }
    }
	
	public interface OnGestureResult {
        public void onGestureResult(int direction);
    }
	
	private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            float x = e2.getX() - e1.getX();
            float y = e2.getY() - e1.getY();
          //限制必须得划过屏幕的1/4才能算划过 
            float x_limit = screen.widthPixels / 8;
            float y_limit = screen.heightPixels / 8;
            float x_abs = Math.abs(x);
            float y_abs = Math.abs(y);
            Log.d("navy", "x="+x+",y="+y+",x_abs="+x_abs+",y_abs="+y_abs);
            if(x_abs >= y_abs){
                //gesture left or right
                if(x > x_limit || x < -x_limit){
                    if(x>0){
                        //right
                        doResult(GESTURE_RIGHT); 
                    }else if(x<=0){
                        //left
                        doResult(GESTURE_LEFT); 
                    }
                }
            }else{
                //gesture down or up
                if(y > y_limit || y < -y_limit){
                    if(y>0){
                        //down
                        doResult(GESTURE_DOWN); 
                    }else if(y<=0){
                        //up
                        doResult(GESTURE_UP); 
                    }
                }
            }
            return true;
        }

		@Override
		public boolean onDown(MotionEvent e) {
			Log.d("navy", "---------onDown-------");
			
			return super.onDown(e);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
            float x1 = e1.getX();
            float y1 = e1.getY();
            float x2 = e2.getX();
            float y2 = e2.getY();
			Log.d("navy", "---------onScroll-------x1="+x1+",y1="+y1+",x2="+x2+",y2="+y2);
			return super.onScroll(e1, e2, distanceX, distanceY);
		}

		@Override
		public void onLongPress(MotionEvent e) {
			Log.d("navy", "---------onLongPress-------");
			super.onLongPress(e);
		}
        
        
    };
}
